var appControllerLoadings = 0;

function appController() {

    function dataHandle(url, params, callback, async, method, loading) {

        if (!loading) {
           // $("#loading").show();
            libs.showLoading()
            appControllerLoadings += 1;
        }
        if (!method) {
            throw 'method 参数未设置';
        }

        params = params || {};
        async = async || true;

        $.ajax({
            async: async,
           //  url: 'dataManagerWeb/' + url,
            url: url,
            dataType: 'json',
            data: params,
            type: method,
            contentType: 'application/json;charset=UTF-8',
            complete: function (xhr, status) {
               /* if (status == 'error'){
                    _as.jAlert("<p>登录超时，请重新登录！</p>", function() {
                        window.location.href = appApi("LOGOUT");
                    })
                }*/
                if (!loading) appControllerLoadings -= 1;
                if (!appControllerLoadings) $("#loading").hide();
                if (xhr.responseText.indexOf("<!DOCTYPE html>") != -1) {
                    if (xhr.responseText.indexOf("<title>云南公安网安安全监管平台</title>") != -1) {
                        _as.jAlert("<p>登录超时，请重新登录！</p>", function() {
                         window.location.href = appApi("LOGOUT");
                    });
                    return false;
                    }
                    _as.jAlert("<p>对不起，服务器接口出错！</p><p>请联系技术人员！</p>");
                    return false;
                }
                /*if (status == 'error'){
                    _as.jAlert("<p>登录超时，请重新登录！</p>", function() {
                        window.location.href = appApi("LOGOUT");
                    })
                }*/
                var result = JSON.parse(xhr.responseText),
                    code = parseInt(result.status.code),
                    message = result.status.message;
                if (code !== 200 && code !== 900) {
                    if (typeof code == "undefined") {
                        console.log('错误码：0000\n异常信息：频繁操作 操作拒绝');
                        return;
                    }
                    if (result.data == 0) {
                        return _as.jAlert("<p>异常信息：</p><p>"+result.status.message+"</p>");
                    }
                    _as.jAlert("<p>异常信息：</p><p>" + message + "</p>");
                    return;
                } else if (code === 900 || code == 500) { // 0
                    var str = ''

                    result.data.forEach(function (d, i) {
                        str += "<p>" + d.message + "</p>"
                    })
                    _as.jAlert(str, function (dom) {

                        $("#" + dom).focus();
                        _as.shake(dom, _as.fadeOut, 5, 500, "noShowRed");
                    }, result.data[0].id);
                    return;
                } else {
                    callback(result.data);
                }
            }
        });
    }

    return {
        'insert': function (url, params, callback, async, loading) {

            dataHandle(url, JSON.stringify(params), callback, async, 'post',loading);
        },
        'del': function (url, params, callback, async, loading) {

            dataHandle(url, JSON.stringify(params), callback, async, 'delete',loading);
        },
        'update': function (url, params, callback, async, loading) {

            dataHandle(url, JSON.stringify(params), callback, async, 'put',loading);
        },
        'sel': function (url, params, callback,async,loading) {
            dataHandle(url, params, callback, async, 'get', loading);
        }
    };
}